/**
 * game.java
 *
 * Fecha de creación: May 12, 2012,6:47:11 PM
 *
 * Autor: José Ignacio Rivero Dávila
 */

package model;

import Intelligence.AIPlayer;
import controller.DefaultController;
import model.Board;
import model.Player;
import util.Difficulty;
import util.Movement;
import util.MovementNotAllowedException;
import util.Position;


public class Game extends AbstractModel{
    AIPlayer aiplayer;
    Player human;
    private boolean whoseTurn;
    final static boolean humanColor = Player.BLACK;
    Board board;
    Movement lastMovement;
    DefaultController controller;
    
    public Game(Difficulty difficulty, DefaultController controller){
        this(difficulty);
        this.controller = controller;
        
        controller.addModel(human);
        
        controller.addModel(aiplayer);
        
        controller.addModel(board);
    }
    public Game(DefaultController controller){
        this();
        this.controller = controller;
        
        controller.addModel(human);
        
        controller.addModel(aiplayer);
        
        controller.addModel(board);
    }
    public Game(Difficulty difficulty){
        this();
        human = new Player();
        aiplayer = new AIPlayer(difficulty, !humanColor);
        lastMovement = null;
        
    }
    public Game(){
        human = new Player();
        aiplayer = new AIPlayer();
        lastMovement = null;
        board = new Board();
        whoseTurn = humanColor;
        
        
    }
    public Difficulty getDifficulty(){
        return aiplayer.getDifficulty();
    }
    public void setDifficulty(Difficulty difficulty){
        aiplayer.setDifficulty(difficulty);
    }
    public void humanMoves(Movement move)throws MovementNotAllowedException{
      if(whoseTurn!=Game.humanColor){
          throw new MovementNotAllowedException("It's the computer's turn");
      } 
      board.move(move); 
      Movement oldLastMovement = lastMovement;
      lastMovement = move;
      firePropertyChange("lastMovement", oldLastMovement,lastMovement);
      whoseTurn=!humanColor;
      firePropertyChange("whoseTurn", humanColor, !humanColor);
    }
    public void aiMove()throws MovementNotAllowedException{
        Movement move;
        
        move = aiplayer.getNextMove(board); 
        
            if(whoseTurn==humanColor){
                throw new MovementNotAllowedException("It's the human's turn"); 
            }
            board.move(move);
       
        Movement oldLastMovement = lastMovement;
        lastMovement = move;
        firePropertyChange("lastMovement", oldLastMovement,lastMovement);
        whoseTurn=humanColor;
        firePropertyChange("whoseTurn", !humanColor, humanColor);
    }
    public static void main(String[] args) {
        Game game = new Game(Difficulty.MEDIUM);
        System.out.println(game.board);
        Movement move = new Movement(new Position(5,0), new Position (5,2));
        
        try{
            game.humanMoves(move);
            game.aiMove();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println(game.board);
    }
    public String toString(){
        return board.toString();
    }
   
   
    
    
    
    
    

}
